package com.admin.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.admin.common.annotation.Log;
import com.admin.common.core.controller.BaseController;
import com.admin.common.core.domain.R;
import com.admin.common.core.page.TableDataInfo;
import com.admin.common.enums.BusinessType;
import com.admin.common.utils.poi.ExcelUtil;
import com.admin.system.domain.SysOperLog;
import com.admin.system.domain.vo.SysOperLogQuery;
import com.admin.system.service.SysOperLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 操作日志记录
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/log/operate")
public class SysOperlogController extends BaseController {

    private final SysOperLogService operLogService;

    /**
     * 获取操作日志记录列表
     */
    @SaCheckPermission("system:operlog:list")
    @PostMapping("/list")
    public TableDataInfo<SysOperLog> list(@RequestBody SysOperLogQuery operLog) {
        return operLogService.selectPageOperLogList(operLog);
    }

    /**
     * 获取操作日志详情
     */
    @SaCheckPermission("system:operlog:detail")
    @PostMapping("/detail/{id}")
    public R<SysOperLog> list(@PathVariable Long id) {
        return R.ok(operLogService.selectOperLogById(id));
    }

    /**
     * 导出操作日志记录列表
     */
    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @SaCheckPermission("system:operlog:export")
    @PostMapping("/export")
    public void export(@RequestBody SysOperLogQuery operLog, HttpServletResponse response) {
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        ExcelUtil.exportExcel(list, "操作日志", SysOperLog.class, response);
    }

    /**
     * 批量删除操作日志记录
     * @param ids 日志ids
     */
    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @SaCheckPermission("system:operlog:delete")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(operLogService.deleteOperLogByIds(ids));
    }

    /**
     * 清理操作日志记录
     */
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @SaCheckPermission("system:operlog:delete")
    @PostMapping("/clean")
    public R<Void> clean() {
        operLogService.cleanOperLog();
        return R.ok();
    }
}
